package trace

import (
	"context"
	"go.opentelemetry.io/otel/sdk/trace"
	trace2 "go.opentelemetry.io/otel/trace"
)

type MyTracerProvider struct {
	NopProvider trace2.TracerProvider
	Provider    *trace.TracerProvider
	Kind        trace2.SpanKind
}

func (m *MyTracerProvider) Tracer(name string, options ...trace2.TracerOption) trace2.Tracer {
	return &MyTracer{
		nopTracer: m.NopProvider.Tracer(name, options...),
		tracer:    m.Provider.Tracer(name, options...),
		kind:      m.Kind,
	}
}

type MyTracer struct {
	nopTracer trace2.Tracer
	tracer    trace2.Tracer
	kind      trace2.SpanKind
}

func (m *MyTracer) Start(ctx context.Context, spanName string, opts ...trace2.SpanStartOption) (context.Context, trace2.Span) {
	enable, ok := ctx.Value("Enable").(bool)
	opts = append(opts, trace2.WithSpanKind(m.kind))
	if ok && enable {
		return m.tracer.Start(ctx, spanName, opts...)
	}
	return m.nopTracer.Start(ctx, spanName, opts...)
}
